/*
 * Tercero.java
 *
 * Created on 28 de abril de 2008, 03:50 PM
 *
 * To change this template, choose Tools | Options and locate the template under
 * the Source Creation and Management node. Right-click the template and choose
 * Open. You can then make changes to the template in the Source Editor.
 */

package score;

import java.sql.ResultSet;
import java.sql.SQLException;
import score.utils.DataBaseManager;

/**
 *
 * @author analista4
 */
public class Tercero {
    
    /** Creates a new instance of Tercero */
    private String Apellido1;
    private String Apellido2;
    private String nombre;
    private long nit;
    private DataBaseManager dbm;
    
    public Tercero(long nit, String ap1, String ap2, String nombre, DataBaseManager dbm) {
        this.nit = nit;
        this.nombre = nombre.trim();
        this.dbm=dbm;
        this.Apellido1=ap1.trim();
        this.Apellido2=ap2.trim();
    }
        
    public String getNombre() {
        return nombre;
    }
    
    public void setNombre(String nombre) {
        this.nombre = nombre;
    }
    
    public long getNit() {
        return nit;
    }
    
    public void setNit(long nit) {
        this.nit = nit;
    }
    
    public DataBaseManager getDbm() {
        return dbm;
    }
    
    public void setDbm(DataBaseManager dbm) {
        this.dbm = dbm;
    }
    
    public boolean isBanned(){
        return false;
        /*try{
            ResultSet rs=dbm.executeQuery("SELECT NOMBRE FROM sig.SIPLA_NIT_VETADO WHERE NUM_IDENT1 ="+nit+"   AND COD_ESTADO=1  WITH UR");
            if(rs!=null&&rs.next()){
                this.logBanned();
                rs.close();
                return true;
            }else return false;
        }catch(SQLException sqle){
            dbm.rollback();
            return false;
        }*/
        
    }
    
    private void logBanned()throws SQLException{
        try{
            dbm.updateQuery("INSERT INTO SIG.LOGS_SIPLA (USUARIO, NIT, PROGRAMA,FECHA,HORA) VALUES ('"+getDbm().getUsername()+"', "+nit+",'SAAT',CURRENT DATE, CURRENT TIME)");
            dbm.commit();
        }catch(SQLException sqle){
            dbm.rollback();
            System.out.println("Error al intentar dejar log de nit vetado.");
            throw sqle;            
        }
    }
    
    public boolean isNombreOK()throws SQLException,Exception{
        return true;
        /*ResultSet rs;
        boolean cond1,cond2;
        String nit=String.valueOf(this.nit);
        try{
            //Verificando que el nit YA existe...
            rs=dbm.executeQuery("SELECT NIT, NOMBRE FROM SIG.TERCEROS WHERE NOMBRE='"+getSigNameAll()+"'");
            if(rs.next()&&!nit.equals(rs.getString("NIT"))){
                return false;
            }else{
                return true;
            }
        }catch(SQLException sqle){
            throw sqle;
        }catch(Exception e){
            throw e;
        }finally{
            dbm.rollback();
        }*/
    }
    
    public String getNombresSig()throws Exception,SQLException{        
        return sigFormatName(getCorrectName(nombre));
    }
    
    private String sigFormatName(String nomb)throws Exception,SQLException{
        nomb=nomb.replaceAll("\'", "'||CHR(39)||'");
        String formatted_name="";
        ResultSet rs=dbm.executeQuery("SELECT SCR.SIG_NEIM_ESPLIT('"+nomb+"') FROM SYSIBM.SYSDUMMY1");
        if(rs!=null&&rs.next()){
            formatted_name=rs.getString(1);
        }        
        rs.close();
        return formatted_name;
    }
    
    public String getSigNameAll()throws Exception,SQLException{
        return getApellidosSig()+getNombresSig();
    }
    
    private String getCorrectName(String nombre){
        String[] vec=nombre.split(" ",2);
        if(vec.length>1){
            return vec[0]+" "+vec[1];
        }else
        return nombre;
    }
    
    public String getApellidosSig()throws Exception,SQLException{
        return sigFormatName(this.Apellido1+" "+this.Apellido2);
    }
}